package com.qdxwx.manage.controller.system;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.system.SystemMsgService;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.system.SystemMsg;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 站内信Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/system/msg")
public class SystemMsgController extends BaseController {

    @Autowired
    private SystemMsgService systemMsgService;

    @Permissions("msg:list")
    @GetMapping("list.html")
    public String teacher() {
        return "system/msg-list";
    }

    @Permissions("msg:list")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        Integer status = TKConvert.toInteger(request.getParameter("status"), null);
        String searchKey = TKString.trim(request.getParameter("searchKey"));
        SystemMsg systemMsg = new SystemMsg();
        if(TKString.isNotEmpty(searchKey)){
            if (TKConvert.toInteger(searchKey) > 0){
                systemMsg = systemMsgService.get(TKConvert.toInteger(searchKey));
                if (systemMsg == null) return TableDataInfo.error("找不到指定id的信息");
                List<SystemMsg> list = new ArrayList<>();
                list.add(systemMsg);
                return TableDataInfo.success(list, 1);
            }else {
                systemMsg.setContent(searchKey);
            }
        }
        systemMsg.setStatus(status);
        List<SystemMsg> list = systemMsgService.getList(systemMsg, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(systemMsgService.count(systemMsg));
        return rspData;
    }

}
